'use strict';

module.exports = app => {
  const { STRING, INTEGER, DATE } = app.Sequelize;

  const QuestionCategory = app.model.define('question_category', {
    category_id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
      comment: '分类编号',
    },
    category_name: {
      type: STRING(100),
      allowNull: false,
      comment: '分类名称',
    },
    created_at: {
      type: DATE,
      allowNull: false,
      comment: '创建时间',
    },
  }, {
    tableName: 'question_category',
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: false,
    comment: '题目分类表',
  });

  // 关联关系
  QuestionCategory.associate = function() {
    // 一个分类可以有多个题目
    app.model.QuestionCategory.hasMany(app.model.Question, {
      foreignKey: 'category_id',
      as: 'questions',
    });
  };

  return QuestionCategory;
};
